package com.all.widget.time;

//import com.okay.eimpuilibrary.R;


import com.all.ui.R;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;

public class DateSetter extends DatePicker {

	public DateSetter(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		changeDatePickerButtons();
	}

	public DateSetter(Context context, AttributeSet attrs) {
		super(context, attrs);
		changeDatePickerButtons();
	}

	public DateSetter(Context context) {
		super(context);
		changeDatePickerButtons();
	}

	private void changeDatePickerButtons() {
		LinearLayout numberPicker = null;
		int index = 0;
		if (android.os.Build.VERSION.SDK_INT >= 14) {
			for (int i = 0; i < super.getChildCount(); i++) {
				for (int j = 0, len = ((LinearLayout) super.getChildAt(i))
						.getChildCount(); j < len - 1; j++) {
					for (int j2 = 0, len1 = ((ViewGroup) ((LinearLayout) super
							.getChildAt(i)).getChildAt(j)).getChildCount(); j2 < len1; j2++) {
						index = j2;
						numberPicker = (LinearLayout) ((LinearLayout) ((LinearLayout) super
								.getChildAt(i)).getChildAt(j)).getChildAt(j2);

						changeNumberPicker(numberPicker, index, len1);
					}
				}
			}
		} else {
			LinearLayout numberPickerParent = (LinearLayout) super
					.getChildAt(0);
			for (int i = 0, len = numberPickerParent.getChildCount(); i < len; i++) {
				index = i;
				numberPicker = (LinearLayout) numberPickerParent.getChildAt(i);

				changeNumberPicker(numberPicker, index, len);
			}
		}
	}

	private void changeNumberPicker(LinearLayout numberPicker, int index,
			int len) {
		Object c1 = numberPicker.getChildAt(0);
		((ImageButton) c1)
				.setBackgroundResource(R.drawable.date_setter_dialog_cancel_button_bg);
		((ImageButton) c1).setImageResource(R.drawable.icon_date_add);
		LinearLayout.LayoutParams lp1 = (LinearLayout.LayoutParams) ((ImageButton) c1)
				.getLayoutParams();

		Object c2 = numberPicker.getChildAt(1);
		((EditText) c2).setBackgroundColor(Color.parseColor("#4ba6e5"));
		((EditText) c2).setTextColor(Color.WHITE);
		((EditText) c2).setTextSize(TypedValue.COMPLEX_UNIT_SP, 24);
		((EditText) c2).setSelectAllOnFocus(true);
		LinearLayout.LayoutParams lp2 = (LinearLayout.LayoutParams) ((EditText) c2)
				.getLayoutParams();

		Object c3 = numberPicker.getChildAt(2);
		((ImageButton) c3)
				.setBackgroundResource(R.drawable.date_setter_dialog_cancel_button_bg);
		((ImageButton) c3).setImageResource(R.drawable.icon_date_reduce);
		LinearLayout.LayoutParams lp3 = (LinearLayout.LayoutParams) ((ImageButton) c3)
				.getLayoutParams();

		if (getResources().getDisplayMetrics().densityDpi == DisplayMetrics.DENSITY_MEDIUM) {
			lp1.height = 40;
			lp2.height = 40;
			lp3.height = 40;
			lp1.width = 80;
			lp2.width = 80;
			lp3.width = 80;
			if (index < len - 1) {
				lp1.rightMargin = 10;
				lp2.rightMargin = 10;
				lp3.rightMargin = 10;
			}
		} else if (getResources().getDisplayMetrics().densityDpi == DisplayMetrics.DENSITY_HIGH) {
			lp1.height = 60;
			lp2.height = 60;
			lp3.height = 60;
			lp1.width = 120;
			lp2.width = 120;
			lp3.width = 120;
			if (index < len - 1) {
				lp1.rightMargin = 15;
				lp2.rightMargin = 15;
				lp3.rightMargin = 15;
			}
		} else if (getResources().getDisplayMetrics().densityDpi == DisplayMetrics.DENSITY_XHIGH) {
			lp1.height = 80;
			lp2.height = 80;
			lp3.height = 80;
			lp1.width = 160;
			lp2.width = 160;
			lp3.width = 160;
			if (index < len - 1) {
				lp1.rightMargin = 20;
				lp2.rightMargin = 20;
				lp3.rightMargin = 20;
			}
		}
		((ImageButton) c1).setLayoutParams(lp1);
		((EditText) c2).setLayoutParams(lp2);
		((ImageButton) c3).setLayoutParams(lp3);
	}

}
